package mvp.com.bbc.famvp.mvp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import mvp.com.bbc.famvp.R;
import mvp.com.bbc.famvp.base.BaseActivity;
import mvp.com.bbc.famvp.config.ConstantsImageUrl;

/**
 * Created by ${cbf} on 2017/11/3.
 * 启动页
 */

public class SplashActivity extends BaseActivity {
    @BindView(R.id.splash_iv_pic)
    ImageView mIvPic;
    @BindView(R.id.splash_tv_jump)
    TextView splashTvJump;
    @BindView(R.id.splash_iv_defult_pic)
    ImageView splashIvPic;
    @BindView(R.id.activity_splash)
    RelativeLayout activitySplash;
    private boolean isIn;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        int i = new Random().nextInt(ConstantsImageUrl.TRANSITION_URLS.length);
        // 先显示默认图
        splashIvPic.setImageDrawable(getResources().getDrawable(R.drawable.img_transition_default));
        Glide.with(this)
                .load(ConstantsImageUrl.TRANSITION_URLS[i])
                .placeholder(R.drawable.img_transition_default)
                .error(R.drawable.img_transition_default)
                .into(mIvPic);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                splashIvPic.setVisibility(View.GONE);
            }
        }, 1500);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toMainActivity();
            }
        }, 3500);

    }

    private void toMainActivity() {
        if (isIn) {
            return;
        }
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.screen_zoom_in, R.anim.screen_zoom_out);
        finish();
        isIn = true;
    }
    @OnClick(R.id.splash_tv_jump)
    public void onClick() {
        toMainActivity();
    }

}
